LIBRARY ieee;
USE ieee.std_logic_1164.all;
USE ieee.std_logic_signed.all;
USE ieee.numeric_std.all; 

ENTITY data_mem IS
	PORT ( Tick				  : IN STD_LOGIC_VECTOR(2 DOWNTO 0);
		   Address, WriteData : IN STD_LOGIC_VECTOR(7 DOWNTO 0);
		   ControlSignal	  : IN STD_LOGIC_VECTOR(1 DOWNTO 0);
		   ReadData			  : OUT STD_LOGIC_VECTOR(7 DOWNTO 0));
END data_mem;

ARCHITECTURE Behavior OF data_mem IS
	
	SIGNAL rdata		: STD_LOGIC_VECTOR(7 DOWNTO 0);
	SIGNAL memory		: STD_LOGIC_VECTOR(0 TO 31);
	
BEGIN
	-- 00: sem execucao
	-- 01: sinal de leitura
	-- 10: sinal de escrita
	ReadData <= rdata;
	dataMemory: PROCESS(ControlSignal)
	BEGIN
		--memory(0 to 7) <= "00000011";				-- hand test
		--memory(8 to 15) <= "00001100";			-- hand test
		--memory(16 to 23) <= "00110000";			-- hand test
		--memory(24 to 31) <= "11000000";			-- hand test
		--CASE Tick IS
			--WHEN "111" =>
				CASE ControlSignal IS
					WHEN "00" => -- sai imediato
						rdata <= Address;
					WHEN "01" => -- leitura
						rdata(7) <= memory(to_integer(unsigned(Address)));
						rdata(6) <= memory(to_integer(unsigned(Address)) + 1);
						rdata(5) <= memory(to_integer(unsigned(Address)) + 2);
						rdata(4) <= memory(to_integer(unsigned(Address)) + 3);
						rdata(3) <= memory(to_integer(unsigned(Address)) + 4);
						rdata(2) <= memory(to_integer(unsigned(Address)) + 5);
						rdata(1) <= memory(to_integer(unsigned(Address)) + 6);
						rdata(0) <= memory(to_integer(unsigned(Address)) + 7);
					WHEN "10" => -- escrita
						memory(to_integer(unsigned(Address))) <= WriteData(7);
						memory(to_integer(unsigned(Address)) + 1) <= WriteData(6);
						memory(to_integer(unsigned(Address)) + 2) <= WriteData(5);
						memory(to_integer(unsigned(Address)) + 3) <= WriteData(4);
						memory(to_integer(unsigned(Address)) + 4) <= WriteData(3);
						memory(to_integer(unsigned(Address)) + 5) <= WriteData(2);
						memory(to_integer(unsigned(Address)) + 6) <= WriteData(1);
						memory(to_integer(unsigned(Address)) + 7) <= WriteData(0);
					WHEN "11" => -- sai resultado da ula
						rdata <= WriteData;
				END CASE;
			--WHEN OTHERS =>
				--rdata <= "00000000";
		--END CASE;

	END PROCESS;
END Behavior;